<?php namespace Admin\Common;


/**
 * TODO: 网络RPC 工具类
 * @author pom <pengsuangqi@aomai.com.au>
 */
class POMNetTools{

  // TODO: 构建参数验证 标志
  static public function BuildSign($APPSecret,$Parmas=array())
  {
    ksort($Parmas);
    $str = "";
    foreach ($Parmas as $key => $value)
    {
      $str .= $key.$value;
    }
    $str .= $APPSecret;

    return md5($str);
  }

  // TODO: 构建参数验证 URL
  static public function BuildUrlForSign($APPKey,$Parmas=array())
  {
    // ksort($Parmas);
    $str = "&";
    foreach ($Parmas as $key => $value)
    {
      $str .= $key."=".$value."&";
    }
    $str .= "app_key=".$APPKey;

    return $str;
    // return md5($str);
  }

  // TODO: request 处理
  static public function I($data,$default)
  {
    return I("request.".$data,$default);
  }


  // TODO: 程序内部网络操作时往往无法直接输出  使用这个函数输出到文件查看
  static public function fileDebug($data,$filename)
  {
    if( !isset($filename) || empty($filename ) ) $filename = "log_".date("Y-m-d").".txt";
    ob_start();

    if( is_array($data) ){
      print_r($data);
    }else{
      echo $data;
    }

    $str = ob_get_clean();
    file_put_contents( dirname(__FILE__)."/".$filename, "\r\n----------------------------\r\n" , FILE_APPEND);
    file_put_contents( dirname(__FILE__)."/".$filename,  date("Y-m-d H:i:s")."\r\n" , FILE_APPEND);
    file_put_contents( dirname(__FILE__)."/".$filename, $str , FILE_APPEND);
  }

  /**
  * 将二维数组通过指定的 key 去重
  *
  * @param array $arr 要去重的数组
  * @param array $by  指定key去重,该参数不指定将返回 array()
  * @return array
  */
  static public function array_multunique($arr,$by = array()) {
         $temp = array();
         foreach($arr as $key => $val) {
                 foreach($by as $v) {
                         $temp[$key] .= isset($val[$v]) ? $val[$v] : '';
                 }
         }
         return array_intersect_key($arr,array_unique($temp));
  }


  static public function buildBizUrl($Module,$Action,$params=array() )
  {
    $url = C("BIZ_SERVICE_PREURL")."/".$Module.".ashx?Action=".$Action;
    if( isset($params) && is_array($params) ){
      foreach ($params as $key => $value) {
        $url .= "&".$key."=".$value;
      }
    }
    return $url;
  }

	static public function getData($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 0);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		$ret  = curl_exec($ch);
		$info = curl_getinfo($ch);
		$err  = curl_errno($ch);
		curl_close($ch);
		if ( CURLE_OK != $err || 200 != $info["http_code"] ) {
				if( self::$DEBUG ) {
          self::fileDebug($info,"");
        }
				return null;
		}
		return json_decode($ret,true);
	}

	static public function postData($url,$data)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		if( $data != null ){
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		$ret  = curl_exec($ch);
		$info = curl_getinfo($ch);
		$err  = curl_errno($ch);
		curl_close($ch);
		if ( CURLE_OK != $err || 200 != $info["http_code"] ) {
			if( self::$DEBUG ) {
        self::fileDebug($info,"");
			}
			return null;
		}
		return json_decode($ret,true);
	}


  //将excel商品相同的合并
static function merge_quantity($goodsArr){
    $goodsArrNew= array();
    foreach($goodsArr as $key=>$item)
    {
      //判断是否在标题=>数组索引数组中
    if(isset($goodsArrNew[$item['GoodsSn']]))
    {
      //在的话添加数量
       $goodsArrNew[$item['GoodsSn']]['Quantity'] += $goodsArr[$key]['Quantity'];
    }
    else  
    {
      //不在的话存入映射索引
      $goodsArrNew[$item['GoodsSn']]=$item;
      unset($goodsArr[$key]); //删除原来在数组存在的值
      }
    }
    return array_values($goodsArrNew);
}

  static public  $DEBUG  = false;
}















?>
